package com.stud.mc.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.stud.common.annotation.Log;
import com.stud.common.annotation.RequiresPermissions;
import com.stud.common.core.controller.BaseController;
import com.stud.common.core.domain.Result;
import com.stud.common.core.page.TableDataInfo;
import com.stud.common.enums.BusinessType;
import com.stud.common.utils.poi.ExcelUtil;
import com.stud.mc.convert.Convertor;
import com.stud.mc.dto.RepositoryDTO;
import com.stud.mc.domain.Repository;
import com.stud.mc.service.RepositoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 音乐仓库Controller
 *
 * @author stud21
 * @date 2024-10-23
 */
@Tag(name = "音乐仓库")
@RestController
@RequestMapping("/mc/repository")
@RequiredArgsConstructor
public class  RepositoryController extends BaseController {
    private final RepositoryService repositoryService;

    @Operation(summary = "查询音乐仓库分页列表")
    @RequiresPermissions("mc:repository:query")
    @GetMapping("/list")
    public TableDataInfo<RepositoryDTO> list(RepositoryDTO repositoryDTO) {
        Wrapper<Repository> wrapper = Wrappers.lambdaQuery(Convertor.INSTANCE.convert(repositoryDTO));
        startPage();
        List<Repository> list = repositoryService.list(wrapper);
        List<RepositoryDTO> dtoList = list.stream().map(Convertor.INSTANCE::convert).collect(Collectors.toList());
        return getDataTable(dtoList);
    }

    @Operation(summary = "导出音乐仓库列表")
    @RequiresPermissions("mc:repository:export")
    @Log(title = "音乐仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RepositoryDTO repositoryDTO) {
        Wrapper<Repository> wrapper = Wrappers.lambdaQuery(Convertor.INSTANCE.convert(repositoryDTO));
        List<Repository> list = repositoryService.list(wrapper);
        ExcelUtil<Repository> util = new ExcelUtil<>(Repository.class);
        util.exportExcel(response, list, "音乐仓库数据");
    }

    @Operation(summary = "获取音乐仓库详细信息")
    @RequiresPermissions("mc:repository:query")
    @GetMapping(value = "/{id}")
    public Result<RepositoryDTO> getInfo(@PathVariable("id") Long id) {
        Repository repository = repositoryService.getById(id);
        return Result.ok(Convertor.INSTANCE.convert(repository));
    }

    @Operation(summary = "新增音乐仓库")
    @RequiresPermissions("mc:repository:add")
    @Log(title = "音乐仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Boolean> add(@Validated @RequestBody RepositoryDTO repositoryDTO) {
        return Result.ok(repositoryService.save(Convertor.INSTANCE.convert(repositoryDTO)));
    }

    @Operation(summary = "修改音乐仓库")
    @RequiresPermissions("mc:repository:edit")
    @Log(title = "音乐仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Boolean> edit(@Validated @RequestBody RepositoryDTO repositoryDTO) {
        return Result.ok(repositoryService.update(Convertor.INSTANCE.convert(repositoryDTO)));
    }

    @Operation(summary = "删除音乐仓库")
    @RequiresPermissions("mc:repository:remove")
    @Log(title = "音乐仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Boolean> remove(@PathVariable Long[] ids) {
        return Result.ok(repositoryService.removeByIds(ids));
    }
}
